import { Component, OnInit, Injector, Input, Output, EventEmitter } from '@angular/core';
import { DataDictionaryService } from 'services';
import { AppComponentBase } from '@shared/component-base/app-component-base';

@Component({
    selector: 'app-group-list',
    templateUrl: './group-list.component.html',
    styleUrls: ['./group-list.component.less'],
    providers: [DataDictionaryService],
})
export class GroupListComponent extends AppComponentBase implements OnInit {
    _type: any;
    @Input()
    set type(type: any) {
        if (!this._type || this._type != type) {
            this._type = type;
            this.getGroups();
        }
    }
    get type(): any { return this._type; }
    @Output() groupSelected = new EventEmitter<any>();
    @Input() groupName: string = '';
    loading = false;
    groups = [];

    constructor(
        injector: Injector,
        private dataDictionaryService: DataDictionaryService,
    ) {
        super(injector);
    }

    ngOnInit() {
        //this.getGroups();
    }

    getGroups() {
        this.dataDictionaryService.getDataGroups(this.type).subscribe((result) => {
            this.groups = result;
            if (this.groups.length > 0) {
                this.showDetail(this.groups[0]);//默认选中第一个
            }
        });
    }

    showDetail(group: any) {
        this.groupSelected.emit(group);
    }
}
